/*
 * CS_REQ_SELF_DEAD.cpp
 *
 *  Created on: 2013. 10. 12.
 *      Author: jjaehuny
 */




#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"
#include "../../NLogic/Monster.h"
#include "../../NLogic/Space.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_BATTLE_RECONNECT_INFO(User * pUser, ::Network::Packet *pPacket)
{
	/*
	 *
	/// 재접속시 전투맵(던젼, PVP, 배틀로얄)
	CS_BATTLE_RECONNECT_INFO	0x0528
	{

	}

	SC_BATTLE_RECONNECT_INFO	0x0529
	{
		/// 맵전환 된 경우 처리를 위해 파티진행 중인 맵과 좌표 정보
		u16 u16map_id;				// 현재 맵 번호
		u16 u16pos_x;				// 세팅될 위치
		u16 u16pos_y;

		/// 유저 or 몬스터 정보
		u8 u8count;
		{
			u32 u32gid;
			u8  u8sender_type;   	// (0 : 주인공 1: 몬스터, 2:소환 주인공 3: 소환 몬스터
			//u8 	u8is_alive;			// 0 죽음 , 1:살아있음
			u16 u16x;				// 현재 좌표
			u16 u16y;
			u32 u32hp;				// 현재 hp
		}
	}
	 */

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_BATTLE_RECONNECT_INFO NULL == pSpace");
		return;
	}

	if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("CS_BATTLE_RECONNECT_INFO NLogic::Space::SpaceType::WORLD");
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::PVP_USER
			|| pSpace->GetData()._spaceType == NLogic::Space::SpaceType::NEW_PVP_ROOM)
	{
		* pAck << static_cast<uint16_t>(PVP_MAP_ID);
	}
	else
	{
		* pAck <<  static_cast<uint16_t>(pSpace->GetData()._mapTid);
	}

	if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::BATTLEROYAL)
	{
		* pAck <<  static_cast<uint16_t>(pUser->GetBattleRoyalBattle()->GetData().x);
		* pAck <<  static_cast<uint16_t>(pUser->GetBattleRoyalBattle()->GetData().y);
		* pAck <<  static_cast<uint16_t>(pUser->GetBattleRoyalBattle()->GetData().degree);
	}
	else
	{
		* pAck <<  static_cast<uint16_t>(pUser->GetBattle()->GetData().x);
		* pAck <<  static_cast<uint16_t>(pUser->GetBattle()->GetData().y);
		* pAck <<  static_cast<uint16_t>(pUser->GetBattle()->GetData().degree);
	}


	if (pUser->GetBattle()->GetData().y <= 0 && pUser->GetBattle()->GetData().x <= 0)
	{
		DEBUGLOG("pUser->GetBattle()->GetData().x : %d, pUser->GetBattle()->GetData().y :%d GID : %d", pUser->GetBattle()->GetData().x, pUser->GetBattle()->GetData().y, pUser->GetUserSeq());
	}

	int total = pSpace->GetUserTree().size();
	total += pSpace->GetMonsterTree().size();
	total += pSpace->GetDeadMonsterTree().size();

	* pAck <<  static_cast<uint8_t>(total);

	std::for_each(pSpace->GetUserTree().begin(), pSpace->GetUserTree().end(), [&](const NLogic::Space::USERTREE::value_type & pair) {
		* pAck << pair.second->GetUserSeq();
		* pAck << static_cast<uint8_t>(0);

		if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::BATTLEROYAL)
		{
			* pAck <<  static_cast<uint16_t>(pair.second->GetBattleRoyalBattle()->GetData().x);
			* pAck <<  static_cast<uint16_t>(pair.second->GetBattleRoyalBattle()->GetData().y);
			* pAck <<  pair.second->GetBattleRoyalBattle()->GetData().HP;
		}
		else
		{
			* pAck <<  static_cast<uint16_t>(pair.second->GetBattle()->GetData().x);
			* pAck <<  static_cast<uint16_t>(pair.second->GetBattle()->GetData().y);
			* pAck << pair.second->GetBattle()->GetData().HP;

			if (0 == pair.second->GetBattle()->GetData().HP)
			{
				DEBUGLOG("%s %d HPISZERO USERSEQ : %d", __FUNCTION__, __LINE__, pair.second->GetUserSeq());
			}
		}
	} );

	std::for_each(pSpace->GetMonsterTree().begin(), pSpace->GetMonsterTree().end(), [&](const NLogic::Space::MONSTERTREE::value_type & pair) {
		* pAck << pair.second->GetData().gid;
		* pAck << static_cast<uint8_t>(pair.second->GetMonsterType());
		* pAck << static_cast<uint16_t>(pair.second->GetData().x);
		* pAck << static_cast<uint16_t>(pair.second->GetData().y);
		* pAck << pair.second->GetData().hp;
	} );

	std::for_each(pSpace->GetDeadMonsterTree().begin(), pSpace->GetDeadMonsterTree().end(), [&](const NLogic::Space::MONSTERTREE::value_type & pair) {
		* pAck << pair.second->GetData().gid;
		* pAck << static_cast<uint8_t>(pair.second->GetMonsterType());
		* pAck << static_cast<uint16_t>(0);
		* pAck << static_cast<uint16_t>(0);
		* pAck << static_cast<uint32_t>(0);
	} );

	pAck->MakePacket(CMD_SC_BATTLE_RECONNECT_INFO);
	pUser->Send(pAck);

//	pSpace->SendAll(pAck);
//
//	NLogic::Monster * pMonster = pSpace->GetMonster(u32gid);
//	if (pMonster != NULL)
//	{
//		pSpace->RemoveMonster(u32gid);
//	}


}

} /*NCommand*/ } /*ProjectZ*/
